#!/usr/bin/env bash
# shellcheck disable=SC1091

# 检查是否在容器中运行
if [[ -d "/host/proc/1/" ]]; then
    source /apps/gitrce/hook/singleton.sh "$0"
fi

# 处理休眠模式
if [[ "$1" == "sleep" ]]; then
    source /apps/gitrce/hook/sleep.sh "$0" 1 100
fi

__init_data() {
    local _currTime
    _currTime="$(date -d "-60 minute" +"%Y-%m-%d %H")"
    
    local _data
    _data=$(tail -n130 /apps/data/network/netflow/proc_net_dev.jsons)
    
    for i in $(seq 0 12); do
        local time_suffix=$(printf "%02d" $((i * 5)))
        eval "_d_${i}=\$(echo \"\$_data\" | grep \"\$_currTime:${time_suffix}\" | jq -r '.data' | base64 -d)"
    done
}

__line_speed() {
    local _line="$1"
    local _tx1 _tx2 _s
    _tx1=$(echo "$_d_a" | grep "$_line" | awk '{print $10}')
    _tx2=$(echo "$_d_b" | grep "$_line" | awk '{print $10}')

    if (( _tx1 > _tx2 )); then
        _s=$(echo "($_tx1 - $_tx2) * 1.08" | bc)
    else
        _s=0
    fi
    echo "$_s"
}

__line_speed_report() {
    local _line="$1"
    local _speeds=()
    
    for i in $(seq 1 12); do
        _d_a=${_d_$((i + 1))}
        _d_b=${_d_$i}
        _speeds+=("$(__line_speed "$_line")")
    done

    local _mac
    _mac=$(ip a show "$_line" | grep 'link/ether' | awk '{print $2}')
    local _ipmi
    _ipmi=$(ipmitool lan print 2>/dev/null | awk -F ': ' '/IP Address/{print $2}')

    local _data
    _data="$(date -d "-60 minute" +"%Y%m%d"),$(date -d "-60 minute" +"%Y%m%d%H"),107,9fcdcca8-3585-11ed-a775-b4fbf97dd08e,$ISP,$_mac,$(IFS=,; echo "${_speeds[*]}"),1,$DEVICE_TYPE"
    
    echo "开始上报 $(date +"%Y-%m-%d %T")"
    echo "$_data" >>/apps/data/network/netflow/reported.json

    local _json
    _json=$(jq -n \
        --arg v "$(date +"%Y-%m-%d %T")" \
        --arg v "$(date '+%s')" \
        --arg v "$(date -d "-60 minute" +"%Y%m%d%H")" \
        --arg v "$_ipmi" \
        --arg v "$ISP" \
        --arg v "${_speeds[0]}" \
        --arg v "${_speeds[1]}" \
        --arg v "${_speeds[2]}" \
        --arg v "${_speeds[3]}" \
        --arg v "${_speeds[4]}" \
        --arg v "${_speeds[5]}" \
        --arg v "${_speeds[6]}" \
        --arg v "${_speeds[7]}" \
        --arg v "${_speeds[8]}" \
        --arg v "${_speeds[9]}" \
        --arg v "${_speeds[10]}" \
        --arg v "${_speeds[11]}" \
        '{
            time: [$v, $v, $v],
            ipmi: $v,
            isp: $v,
            data: {
                minute05: $v,
                minute10: $v,
                minute15: $v,
                minute20: $v,
                minute25: $v,
                minute30: $v,
                minute35: $v,
                minute40: $v,
                minute45: $v,
                minute50: $v,
                minute55: $v,
                minute60: $v
            }
        }' | jq -c
    )
    echo "$_json" >>/apps/data/network/netflow/reported.json

    curl -s -H "X-Seq:1" -H "access_id:10f878ca-a65a-11ea-8a8a-6c0b84af356a" -H "need_response:true" -d "$_data" -D- http://pcdnlog.qq.com
}

__main() {
    # 每小时6分钟后上报数据
    if (($(date +'%M') > 6)); then
        source /apps/data/env/env.config
        if [[ -n "$ISP" && -n "$DEVICE_TYPE" ]]; then
            __init_data
            local __nics
            __nics=$(nsenter --mount=/host/proc/1/ns/mnt cat /etc/pcdn/pcdn.conf | grep 'nics' | sed 's/nics\s//g' | xargs -d ',' -n1 | grep -v '^$')
            for item in $__nics; do
                __line_speed_report "$item"
            done
        else
            echo "需要在 env.config 文件配置运营商变量"
            echo "ISP > 运营商对应关系:  1: 电信, 2: 移动, 3: 联通, 4: 其他"
            echo "DEVICE_TYPE > 设备类型对应关系:  1: 盒子, 2: 矿机, 3: 专线，-1: 未知"
        fi
    else
        echo "每小时 0-6 分钟, 数据不完整不允许上报"
    fi
}

__main >>/apps/data/network/netflow/reported.log
